home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / PowerPlant / LProgressIndicator & Friends / Threads App Source / CConsumerThread.cp next >
Encoding:
Text File  |  1996-04-11  |  1.9 KB  |  73 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    CConsumerThread.cp                     ©1996 Metrowerks Inc. All rights reserved.
  3. // =================================================================================
  4.  
  5. #include <LLink.h>
  6. #include <LSharedQueue.h>
  7.  
  8. #include "LThermometerPane.h"
  9.  
  10. #include "CConsumerThread.h"
  11.  
  12.  
  13. // ---------------------------------------------------------------------------------
  14. //        • CConsumerThread
  15. // ---------------------------------------------------------------------------------
  16.  
  17. CConsumerThread::CConsumerThread(
  18.     LSharedQueue    *inQueue,
  19.     LThermometerPane    *inProgressPane )
  20.         : LThread( false ), mQueue( inQueue ), mProgressPane( inProgressPane )
  21. {
  22. }
  23.  
  24.  
  25. // ---------------------------------------------------------------------------------
  26. //        • ~CConsumerThread
  27. // ---------------------------------------------------------------------------------
  28.  
  29. CConsumerThread::~CConsumerThread()
  30. {
  31. }
  32.  
  33.  
  34. // ---------------------------------------------------------------------------------
  35. //        • Run
  36. // ---------------------------------------------------------------------------------
  37.  
  38. void *
  39. CConsumerThread::Run( void )
  40. {
  41.     Int32    theMaxValue;
  42.     Int32    theValue;
  43.     
  44.     // Get the progress bar max value.
  45.     theMaxValue = mProgressPane->GetMaxValue();
  46.     
  47.     // Consume as long as the progress bar isn't full.
  48.     while ( (theValue = mProgressPane->GetCurrentValue()) < theMaxValue ) {
  49.  
  50.         // Get data from the shared queue.
  51.         LLink    *theData = mQueue->Next();
  52.  
  53.         // Just delete it.
  54.         delete theData;
  55.  
  56.         // Increment the progress bar value.
  57.         mProgressPane->SetCurrentValue( theValue + 1 );
  58.         
  59.         // Go to sleep for a while. This makes the consumer
  60.         // a little slower than the producer so we can see
  61.         // date in the shared queue. Try experimenting with
  62.         // this value.
  63.         // Note: there's no need to yield since sleeping
  64.         // will allow other threads to get time.
  65.         Sleep( 50 );
  66.  
  67.     }
  68.     
  69.     Suspend();
  70.     
  71.     return nil;
  72. }
  73.